package com.das.mechanic_base.adapter.camera;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.camera.ImageVideoBean;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.kproduce.roundcorners.RoundFrameLayout;

import java.util.ArrayList;
import java.util.List;

public class X3CameraPhotoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<ImageVideoBean> mList = new ArrayList();

    private List<Boolean> sList = new ArrayList<>();

    private List<ImageVideoBean> mSelectedImages = new ArrayList();

    public interface ItemCallback{
        /**
         * 点击item 选中状态
         * @param position
         */
        void iOnCameraShow(int position);

        /**
         * 选中item出现白框
         * @param path
         */
        void iOnCameraSelectShow(String path,boolean isFirst);
    }

    private ItemCallback itemCallback;
    RecyclerView mRecyclerView;

    public X3CameraPhotoAdapter(Context context, ItemCallback itemCallback,RecyclerView mRecyclerView){
        mContext = context;
        this.mRecyclerView = mRecyclerView;
        this.itemCallback = itemCallback;
    }

    public List<ImageVideoBean> getmSelectedImages() {
        return mSelectedImages;
    }

    public ImageVideoBean getItem(int i) {
        return mList.get(i);
    }

    /**
     * 选择某个图片，改变选择状态
     * @param position
     */
    public void select(int position) {
        if(mSelectedImages.contains(mList.get(position))){
            mSelectedImages.remove(mList.get(position));
        }else{
            mSelectedImages.add(mList.get(position));
        }
        if (mRecyclerView != null){
            mRecyclerView.post(new Runnable() {
                @Override
                public void run() {
                    notifyDataSetChanged();
                }
            });
        } else {
            notifyDataSetChanged();
        }
    }

    /**
     * 通过图片路径设置默认选择
     * @param resultList
     */
    public void setDefaultSelected(List<String> resultList) {
        for(String path : resultList){
            ImageVideoBean image = getImageByPath(path);
            if(image != null){
                mSelectedImages.add(image);
            }
        }
        notifyDataSetChanged();
    }

    private ImageVideoBean getImageByPath(String path){
        if(!X3StringUtils.isListEmpty(mList)){
            for(ImageVideoBean image : mList){
                if(image.path.equalsIgnoreCase(path)){
                    return image;
                }
            }
        }
        return null;
    }

    /**
     * 设置数据集
     * @param images
     */
    public void setData(List<ImageVideoBean> images) {
        sList.clear();
        if(!X3StringUtils.isListEmpty(images)){
            mList = images;
            for (int i = 0; i < mList.size(); i++) {
                sList.add(false);
            }
            if (!X3StringUtils.isListEmpty(mSelectedImages)) {
                int i = mList.indexOf(mSelectedImages.get(0));
                if (i != -1) {
                    sList.remove(i);
                    sList.add(i,true);
                    if (itemCallback != null) {
                        itemCallback.iOnCameraSelectShow(mList.get(i).path,true);
                    }
                }else {
                    sList.remove(0);
                    sList.add(0,true);
                    if (itemCallback != null) {
                        itemCallback.iOnCameraSelectShow(mList.get(0).path,true);
                    }
                }
            }else {
                sList.remove(0);
                sList.add(0,true);
                if (itemCallback != null) {
                    itemCallback.iOnCameraSelectShow(mList.get(0).path,true);
                }
            }
        }else{
            mList.clear();
        }
        notifyDataSetChanged();
    }

    public void clearSelectImages() {
        mSelectedImages.clear();
        notifyDataSetChanged();
    }

    public void addData(List<ImageVideoBean> images){
        this.mList.addAll(images);
        notifyDataSetChanged();
    }

    public List<ImageVideoBean> getList(){
        return mList;
    }

    @Override
    public int getItemViewType(int position) {
        if (X3StringUtils.isListEmpty(mList)) {
            return 0;
        }
        return 1;
    }

    @Override
    public int getItemCount() {
        return X3StringUtils.isListEmpty(mList) ? 1 : mList.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        if (viewType == 0) {
            return new EmptyHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_empty_record_item, parent, false));
        }
        return new CameraPhotoHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_camera_photo_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        if (getItemViewType(position) == 1) {

            CameraPhotoHolder photoHolder = (CameraPhotoHolder) holder;

            photoHolder.itemView.setTag(position);

            if (position < sList.size() && sList.get(position)) {
                photoHolder.fl_img.setStrokeColor(Color.WHITE);
            }else {
                photoHolder.fl_img.setStrokeColor(Color.TRANSPARENT);
            }

            /**
             * 处理图片与视频多选状态
             */
            if(mSelectedImages.contains(mList.get(position))){
                // 设置选中状态
                photoHolder.tv_check.setBackgroundResource(0);
                photoHolder.tv_check.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_photo_check));
                int i = mSelectedImages.indexOf(mList.get(position));
                photoHolder.tv_check.setText((i + 1)+ "");
                photoHolder.v_mask.setVisibility(View.VISIBLE);
            }else{
                // 未选择
                photoHolder.tv_check.setText("");
                photoHolder.tv_check.setBackground(null);
                photoHolder.tv_check.setBackgroundResource(R.mipmap.x3_photo_no_check);
                photoHolder.v_mask.setVisibility(View.GONE);
            }

            if(mList.get(position).path.endsWith(".mp4")) {
                photoHolder.iv_play.setVisibility(View.VISIBLE);
                long duration = mList.get(position).time;
                photoHolder.tv_time.setVisibility(duration > 0 ? View.VISIBLE : View.GONE);
                photoHolder.tv_time.setText("00:"+ (duration< 10 ? "0" + duration : duration));
            }else {
                photoHolder.iv_play.setVisibility(View.GONE);
                photoHolder.tv_time.setVisibility(View.GONE);
            }

            X3GlideNewUtils.loadNormalImage(mContext,mList.get(position).path,photoHolder.iv_img,Color.BLACK);

        }else {
            ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
            layoutParams.width = X3ScreenUtils.getScreenWidth(mContext);
            layoutParams.height = X3ScreenUtils.getScreenHeight(mContext);
            holder.itemView.setLayoutParams(layoutParams);
            EmptyHolder emptyHolder = (EmptyHolder) holder;
            emptyHolder.tv_empty.setText(mContext.getString(R.string.x3_home_no_select));
        }

    }

    class CameraPhotoHolder extends RecyclerView.ViewHolder {
        ImageView iv_img;
        View v_mask;
        TextView tv_check;
        TextView tv_time;
        ImageView iv_play;
        RoundFrameLayout fl_img;

        public CameraPhotoHolder(@NonNull View itemView) {
            super(itemView);
            iv_img = itemView.findViewById(R.id.iv_img);
            v_mask = itemView.findViewById(R.id.v_mask);
            tv_check = itemView.findViewById(R.id.tv_check);
            tv_time = itemView.findViewById(R.id.tv_time);
            iv_play = itemView.findViewById(R.id.iv_play);
            fl_img = itemView.findViewById(R.id.fl_img);

            tv_check.setOnClickListener(v -> {
                int position = Integer.parseInt(itemView.getTag().toString());
                if(itemCallback != null){
                    itemCallback.iOnCameraShow(position);
                }
            });
            itemView.setOnClickListener(v -> {
                int position = Integer.parseInt(v.getTag().toString());
                if(itemCallback != null){
                    itemCallback.iOnCameraSelectShow(mList.get(position).path,false);
                    for (int i = 0; i < sList.size(); i++) {
                        sList.remove(i);
                        sList.add(i,i == position);
                    }
                    notifyDataSetChanged();
                }
            });
        }
    }

    public class EmptyHolder extends RecyclerView.ViewHolder {
        TextView tv_empty;
        public EmptyHolder(@NonNull View itemView) {
            super(itemView);
            tv_empty = itemView.findViewById(R.id.tv_empty);
        }
    }
}
